package com.student.management;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    private static final ArrayList<Student> students = new ArrayList<>();
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("--------学生管理系统--------");
            System.out.println("1. 添加学生");
            System.out.println("2. 查看所有学生");
            System.out.println("3. 删除学生");
            System.out.println("4. 修改学生");
            System.out.println("5. 退出");
            System.out.print("请输入您的选择: ");

            String choice = scanner.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    showAllStudents();
                    break;
                case "3":
                    deleteStudent();
                    break;
                case "4":
                    updateStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理系统，再见！");
                    System.exit(0);
                default:
                    System.out.println("输入有误，请重新输入！");
            }
        }
    }

    // 添加学生
    private static void addStudent() {
        System.out.println("--------添加学生--------");

        String id;
        while (true) {
            System.out.print("请输入学号: ");
            id = scanner.next();
            if (getIndex(id) != -1) {
                System.out.println("该学号已存在，请重新输入！");
            } else {
                break;
            }
        }

        System.out.print("请输入姓名: ");
        String name = scanner.next();

        System.out.print("请输入年龄: ");
        int age = scanner.nextInt();

        System.out.print("请输入住址: ");
        String address = scanner.next();

        Student student = new Student(id, name, age, address);
        students.add(student);
        System.out.println("添加学生成功！");
    }

    // 查看所有学生
    private static void showAllStudents() {
        System.out.println("--------查看所有学生--------");
        if (students.isEmpty()) {
            System.out.println("暂无学生信息，请先添加学生！");
            return;
        }

        System.out.println("学号\t\t姓名\t\t年龄\t\t住址");
        for (Student student : students) {
            System.out.printf("%s\t\t%s\t\t%d岁\t\t%s%n",
                    student.getId(),
                    student.getName(),
                    student.getAge(),
                    student.getAddress());
        }
    }

    // 删除学生
    private static void deleteStudent() {
        System.out.println("--------删除学生--------");
        if (students.isEmpty()) {
            System.out.println("暂无学生信息，请先添加学生！");
            return;
        }

        System.out.print("请输入要删除的学生学号: ");
        String id = scanner.next();
        int index = getIndex(id);

        if (index == -1) {
            System.out.println("该学号不存在，删除失败！");
        } else {
            students.remove(index);
            System.out.println("删除学生成功！");
        }
    }

    // 修改学生
    private static void updateStudent() {
        System.out.println("--------修改学生--------");
        if (students.isEmpty()) {
            System.out.println("暂无学生信息，请先添加学生！");
            return;
        }

        System.out.print("请输入要修改的学生学号: ");
        String id = scanner.next();
        int index = getIndex(id);

        if (index == -1) {
            System.out.println("该学号不存在，修改失败！");
        } else {
            System.out.print("请输入新姓名(原" + students.get(index).getName() + "): ");
            String name = scanner.next();

            System.out.print("请输入新年龄(原" + students.get(index).getAge() + "): ");
            int age = scanner.nextInt();

            System.out.print("请输入新住址(原" + students.get(index).getAddress() + "): ");
            String address = scanner.next();

            Student student = new Student(id, name, age, address);
            students.set(index, student);
            System.out.println("修改学生成功！");
        }
    }

    // 根据学号获取学生在集合中的索引
    private static int getIndex(String id) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
